package com.rzjm.exercise.module.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.rzjm.exercise.common.base.controller.BaseController;
import com.rzjm.exercise.common.base.data.DataTableLayUI;
import com.rzjm.exercise.common.log.annotation.Log;
import com.rzjm.exercise.common.utils.DateUtils;
import com.rzjm.exercise.common.utils.R;
import com.rzjm.exercise.module.domain.PickUpMatter;
import com.rzjm.exercise.module.domain.PickUpPlan;
import com.rzjm.exercise.module.domain.PickUpPlanInfo;
import com.rzjm.exercise.module.domain.Shop;
import com.rzjm.exercise.module.service.PickUpMatterService;
import com.rzjm.exercise.module.service.PickUpPlanInfoService;
import com.rzjm.exercise.module.service.PickUpPlanService;
import com.rzjm.exercise.module.service.ShopService;
import com.rzjm.exercise.sys.system.user.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;


@Controller
@RequestMapping(value = "/module/pickUpPlan",produces ="application/json;charset=UTF-8")
public class PickUpPlanController extends BaseController {
    @Resource
    private ShopService shopService;
    @Resource
    private PickUpPlanService pickUpPlanService;
    @Resource
    private PickUpPlanInfoService pickUpPlanInfoService;
    @Resource
    private PickUpMatterService pickUpMatterService;


    @Log("查询车辆预约页面")
    @GetMapping("")
    @RequiresPermissions("module:pickUpPlan:pickUpPlan")
    String pickUpPlan(Model model) {
        return "module/pickUpPlan/list";
    }


    @Log("查询车辆预约页面")
    @ResponseBody
    @GetMapping("/list")
    public DataTableLayUI<PickUpPlan> list(DataTableLayUI<PickUpPlan> condition) {
        UserDO user = getUser();
        Map<String, Object> searchParams = condition.getSearchParams();
        searchParams.put("shopId",user.getShopId());
        return pickUpPlanService.getListPage(condition);
    }


    @Log("新增车辆预约页面")
    @GetMapping("/add")
    @RequiresPermissions("module:pickUpPlan:add")
    String add(Model model) {
        UserDO userDO = getUser();
        PickUpPlan pickUpPlan = new PickUpPlan();
        QueryWrapper<PickUpMatter> wrapper = new QueryWrapper<>();
        wrapper.eq("shop_id", userDO.getShopId());
        List<PickUpMatter> matterList = pickUpMatterService.list(wrapper);
        model.addAttribute("pickUpPlan", pickUpPlan);
        model.addAttribute("planInfoList", new ArrayList<PickUpPlanInfo>());
        model.addAttribute("matterList", matterList);
        return "module/pickUpPlan/detail";
    }

    @Log("编辑车辆预约页面")
    @GetMapping("/edit/{id}")
    @RequiresPermissions("module:pickUpPlan:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        UserDO userDO = getUser();
        PickUpPlan pickUpPlan = pickUpPlanService.findById(id);
        List<PickUpPlanInfo> planInfoList = pickUpPlanInfoService.selectListByColumn("plan_id", id);
        for (int i = 0; i < planInfoList.size(); i++) {
            PickUpPlanInfo pickUpPlanInfo = planInfoList.get(i);
            pickUpPlanInfo.setInfoTimeRange(DateUtils.getSimpleTime(pickUpPlanInfo.getBeginTime()) + " - " + DateUtils.getSimpleTime(pickUpPlanInfo.getEndTime()));
        }
        QueryWrapper<PickUpMatter> wrapper = new QueryWrapper<>();
        wrapper.eq("shop_id", userDO.getShopId());
        List<PickUpMatter> matterList = pickUpMatterService.list(wrapper);
        model.addAttribute("pickUpPlan", pickUpPlan);
        model.addAttribute("planInfoList", planInfoList);
        model.addAttribute("matterList", matterList);
        return "module/pickUpPlan/detail";
    }

    @Log("新增车辆预约")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("module:pickUpPlan:add")
    public R save(@RequestBody PickUpPlan pickUpPlan) {
        UserDO user = getUser();
        Integer shopId = user.getShopId();
        Shop shop = shopService.findById(shopId);
        pickUpPlan.setCreateTime(new Date());
        pickUpPlan.setShopId(shopId);
        pickUpPlan.setLat(BigDecimal.valueOf(shop.getLatitude()));
        pickUpPlan.setLng(BigDecimal.valueOf(shop.getLongitude()));
        pickUpPlan.setShopId(shopId);
        pickUpPlan.setDistance(3000.0);
        pickUpPlan.setState(1);
        String planDate = pickUpPlan.getPlanDate();
        pickUpPlan.setCarCount( pickUpPlan.getPickUpPlanInfoList().size());
        pickUpPlan.setPlanBeginTime(DateUtils.parseDate(planDate + " 00:00:00"));
        pickUpPlan.setPlanEndTime(DateUtils.parseDate(planDate + " 23:59:59"));
        pickUpPlan.setPlanNum(UUID.randomUUID().toString().replaceAll("-", ""));
        pickUpPlanService.save(pickUpPlan);
        List<PickUpPlanInfo> pickUpPlanInfoList = pickUpPlan.getPickUpPlanInfoList();
        for (int i = 0; i < pickUpPlanInfoList.size(); i++) {
            PickUpPlanInfo pickUpPlanInfo = pickUpPlanInfoList.get(i);
            pickUpPlanInfo.setPlanId(pickUpPlan.getId());
            pickUpPlanInfo.setPlanDate(planDate);
            String beginTime = DateUtils.getSimpleTime(pickUpPlanInfo.getBeginTime());
            String endTime = DateUtils.getSimpleTime(pickUpPlanInfo.getEndTime());
            pickUpPlanInfo.setBeginTime(DateUtils.ConvertDateByformat(planDate + " " + beginTime, "yyyy-MM-dd HH:mm:ss"));
            pickUpPlanInfo.setEndTime(DateUtils.ConvertDateByformat(planDate + " " + endTime, "yyyy-MM-dd HH:mm:ss"));
            pickUpPlanInfo.setShopId(shopId);
            pickUpPlanInfo.setCarCount(1);
            pickUpPlanInfo.setState(0);
        }
        pickUpPlanInfoService.saveBatch(pickUpPlanInfoList);
        return R.ok();
    }


    /**
     * 修改
     */
    @Log("更新车辆预约")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("module:pickUpPlan:edit")
    public R update(@RequestBody PickUpPlan pickUpPlan) {
        UserDO user = getUser();
        Integer shopId = user.getShopId();
        Shop shop = shopService.findById(shopId);
        pickUpPlan.setShopId(shopId);
        pickUpPlan.setLat(BigDecimal.valueOf(shop.getLatitude()));
        pickUpPlan.setLng(BigDecimal.valueOf(shop.getLongitude()));
        pickUpPlan.setShopId(shopId);
        pickUpPlan.setDistance(3000.0);
        String planDate = pickUpPlan.getPlanDate();
        pickUpPlan.setCarCount( pickUpPlan.getPickUpPlanInfoList().size());
        pickUpPlan.setPlanBeginTime(DateUtils.parseDate(planDate + " 00:00:00"));
        pickUpPlan.setPlanEndTime(DateUtils.parseDate(planDate + " 23:59:59"));
        pickUpPlanService.updateById(pickUpPlan);
        QueryWrapper<PickUpPlanInfo> infoQueryWrapper = new QueryWrapper<>();
        infoQueryWrapper.eq("plan_id", pickUpPlan.getId());
        pickUpPlanInfoService.remove(infoQueryWrapper);
        List<PickUpPlanInfo> pickUpPlanInfoList = pickUpPlan.getPickUpPlanInfoList();
        for (int i = 0; i < pickUpPlanInfoList.size(); i++) {
            PickUpPlanInfo pickUpPlanInfo = pickUpPlanInfoList.get(i);
            pickUpPlanInfo.setPlanId(pickUpPlan.getId());
            pickUpPlanInfo.setPlanDate(planDate);
            String beginTime = DateUtils.getSimpleTime(pickUpPlanInfo.getBeginTime());
            String endTime = DateUtils.getSimpleTime(pickUpPlanInfo.getEndTime());
            pickUpPlanInfo.setBeginTime(DateUtils.ConvertDateByformat(planDate + " " + beginTime, "yyyy-MM-dd HH:mm:ss"));
            pickUpPlanInfo.setEndTime(DateUtils.ConvertDateByformat(planDate + " " + endTime, "yyyy-MM-dd HH:mm:ss"));
            pickUpPlanInfo.setShopId(shopId);
            pickUpPlanInfo.setCarCount(1);
            pickUpPlanInfo.setState(0);
        }
        pickUpPlanInfoService.saveBatch(pickUpPlanInfoList);
        return R.ok();
    }


    /**
     * 删除
     */
    @Log("删除车辆预约")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("module:pickUpPlan:remove")
    public R remove(Integer id) {
        UpdateWrapper<PickUpPlan> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("state", 0);
        if (pickUpPlanService.update(wrapper)) {
            return R.ok();
        }
        return R.error();
    }
    /**
     * 发布
     */
    @Log("发布车辆预约")
    @PostMapping("/publish")
    @ResponseBody
    public R publish(Integer id) {
        UserDO user = getUser();
        UpdateWrapper<PickUpPlan> wrapper = new UpdateWrapper<>();
        wrapper.eq("state", 2);
        wrapper.eq("shop_id", user.getShopId());
        wrapper.set("state", 3);
        pickUpPlanService.update(wrapper);
        wrapper.clear();
        wrapper.eq("id", id);
        wrapper.set("state", 2);
        if (pickUpPlanService.update(wrapper)) {
            return R.ok();
        }
        return R.error();
    }
    @Log("结束发布车辆预约")
    @PostMapping("/endPublish")
    @ResponseBody
    public R endPublish(Integer id) {
        UserDO user = getUser();
        UpdateWrapper<PickUpPlan> wrapper = new UpdateWrapper<>();
        wrapper.eq("state", 2);
        wrapper.eq("shop_id", user.getShopId());
        wrapper.set("state", 3);
        if (pickUpPlanService.update(wrapper)) {
            return R.ok();
        }
        return R.error();
    }
    @PostMapping("/isHasPublish")
    @ResponseBody
    public R isHasPublish() {
        UserDO user = getUser();
        UpdateWrapper<PickUpPlan> wrapper = new UpdateWrapper<>();
        wrapper.eq("state", 2);
        wrapper.eq("shop_id", user.getShopId());
        return R.ok(0, pickUpPlanService.count(wrapper));
    }
}
